package com.dzf;

import com.dzf.dao.UserDao;
import com.dzf.entity.Type;
import com.dzf.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableCaching
public class SpringBootBokerApplication {


    public static void main(String[] args) {
        User user = new User();
        user.setUsername("adm");
        user.setPassword(new BCryptPasswordEncoder().encode("123456"));
        user.setName("adm");

        ApplicationContext context = SpringApplication.run(SpringBootBokerApplication.class, args);
        MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);

        //默认用户
        int count = (int) mongoTemplate.count(new Query(), User.class);
        if (count>0)
        {
            List<User> userList = mongoTemplate.find(new Query().addCriteria(Criteria.where("username").is("adm")), User.class);
            if (userList.size()<0)
            {
                mongoTemplate.save(user);
            }
        }else
        {
            mongoTemplate.save(user);
        }

        int count1 = (int) mongoTemplate.count(new Query(), Type.class);
        if (count1<=0)
        {
            List<Type> list = new ArrayList<Type>();
            Type type = new Type();
            type.setName("Java");

            Type type1 = new Type();
            type1.setName("随笔区");

            Type type2 = new Type();
            type2.setName("灌水区");

            list.add(type);
            list.add(type1);
            list.add(type2);
            mongoTemplate.insert(list,Type.class);
        }


    }

}
